package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {


    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {

        //目的地处理:  大攻略绑定目的地
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();  //大攻略关联查询目的地
        detail.setDest(dest);

        //作者 此处忽略

        //摘要 summary
        //方案1: 由运营人员自己编写
        //方案2: 截取文章内容前200个字
        String content = detail.getStrategyContent().getContent();
        if(content.length() > 200){
            detail.setSummary(content.substring(0, 200));
        }else{
            detail.setSummary(content);
        }

        //是否国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        Destination country = toasts.get(0);
        if(country.getId() == 1){  //1:中国id
            detail.setIsabroad(false);
        }else{
            detail.setIsabroad(true);
        }


        if(detail.getId() == null){
            //创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);

            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);

        }else{
            strategyDetailMapper.updateByPrimaryKey(detail);

            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);

        }
        //------------------tags--------------------------------
        //打破关系
        strategyDetailMapper.deleteRelation(detail.getId());
        if(StringUtils.hasLength(tags)){
            String[] strs = tags.split(",");
            for (String name : strs) {
                StrategyTag t = new StrategyTag();
                t.setName(name);
                t.setState(StrategyTag.STATE_NORMAL);
                //优化点: 如果说数据库中该tag已经存在, 怎么处理法
                strategyTagService.saveOrUpdate(t);
                //维护关系
                strategyDetailMapper.insertRelation(detail.getId(), t.getId());
            }
        }









    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long destId) {
        return strategyDetailMapper.selectViewNumTop3(destId);
    }

    @Override
    public void updateReplyNum(Long id, int replynum) {
        strategyDetailMapper.updateReplyNum(id, replynum);
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getAbroadCds() {
        return strategyDetailMapper.getAbroadCds();
    }

    @Override
    public List<StrategyDetail> getUnabroadCds() {
        return strategyDetailMapper.getUnabroadCds();
    }

    @Override
    public List<StrategyDetail> getHotCds() {
        return strategyDetailMapper.getHotCds();
    }
//
//    @Override
//    public void updateStatisVO(StrategyStatisPersistenceVO vo) {
//        strategyDetailMapper.updateStatisVO(vo);
//    }
}
